<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>跨字段实体搜索 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cross_fields_entity_search.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cross_fields_entity_search.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_cross_fields_entity_search.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_cross_fields_entity_search.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="multi-field-search.html">多字段搜索</a></span>
»
<span class="breadcrumb-node">跨字段实体搜索</span>
</div>
<div class="navheader">
<span class="prev">
<a href="most-fields.html">« 多数字段</a>
</span>
<span class="next">
<a href="field-centric.html">字段中心式查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_cross_fields_entity_search"></a>跨字段实体搜索<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/35_Entity_search.asciidoc">edit</a>
</h2>
</div></div></div>
<p>现在讨论一种普遍的搜索模式：跨字段实体搜索（cross-fields entity search）。在如 <code class="literal">person</code> 、 <code class="literal">product</code> 或 <code class="literal">address</code> （人、产品或地址）这样的实体中，需要使用多个字段来唯一标识它的信息。 <code class="literal">person</code> 实体可能是这样索引的：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "firstname":  "Peter",
    "lastname":   "Smith"
}</pre>
</div>
<p>或地址：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "street":   "5 Poland Street",
    "city":     "London",
    "country":  "United Kingdom",
    "postcode": "W1V 3DG"
}</pre>
</div>
<p>这与之前描述的 <a class="xref" href="multi-query-strings.html" title="多字符串查询">多字符串查询</a> 很像，但这存在着巨大的区别。在 <a class="xref" href="multi-query-strings.html" title="多字符串查询">多字符串查询</a> 中，我们为每个字段使用不同的字符串，在本例中，我们想使用 <em>单个</em> 字符串在多个字段中进行搜索。</p>
<p>我们的用户可能想搜索 “Peter Smith” 这个人，或 “Poland Street W1V” 这个地址，这些词出现在不同的字段中，所以如果使用 <code class="literal">dis_max</code> 或 <code class="literal">best_fields</code> 查询去查找 <em>单个</em> 最佳匹配字段显然是个错误的方式。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_简单的方式"></a>简单的方式<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/35_Entity_search.asciidoc">edit</a>
</h3>
</div></div></div>
<p>依次查询每个字段并将每个字段的匹配评分结果相加，听起来真像是 <code class="literal">bool</code> 查询：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "query": {
    "bool": {
      "should": [
        { "match": { "street":    "Poland Street W1V" }},
        { "match": { "city":      "Poland Street W1V" }},
        { "match": { "country":   "Poland Street W1V" }},
        { "match": { "postcode":  "Poland Street W1V" }}
      ]
    }
  }
}</pre>
</div>
<p>为每个字段重复查询字符串会使查询瞬间变得冗长，可以采用 <code class="literal">multi_match</code> 查询，将 <code class="literal">type</code> 设置成 <code class="literal">most_fields</code> 然后告诉 Elasticsearch 合并所有匹配字段的评分：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "query": {
    "multi_match": {
      "query":       "Poland Street W1V",
      "type":        "most_fields",
      "fields":      [ "street", "city", "country", "postcode" ]
    }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_most_fields_方式的问题"></a>most_fields 方式的问题<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/35_Entity_search.asciidoc">edit</a>
</h3>
</div></div></div>
<p>用 <code class="literal">most_fields</code> 这种方式搜索也存在某些问题，这些问题并不会马上显现：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
它是为多数字段匹配 <em>任意</em> 词设计的，而不是在 <em>所有字段</em> 中找到最匹配的。
</li>
<li class="listitem">
它不能使用 <code class="literal">operator</code> 或 <code class="literal">minimum_should_match</code> 参数来降低次相关结果造成的长尾效应。
</li>
<li class="listitem">
词频对于每个字段是不一样的，而且它们之间的相互影响会导致不好的排序结果。
</li>
</ul>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="most-fields.html">« 多数字段</a>
</span>
<span class="next">
<a href="field-centric.html">字段中心式查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>